/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.socialNet.dao.implementations;

import com.bionic.socialNet.dao.interfaces.IProfileDAO;
import com.bionic.socialNet.entities.ProfileData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ann
 */
public class OraProfile extends QueryExecutor implements IProfileDAO {

    private final String INSERT_PROFILE = "insert into profile  (user_id, nickname, fullname, birthday, sex, about, photo)"
            + "values  (%d, '%s', '%s', %tD, %d, '%s', null)";
    //(v_user_id, v_nickname, v_fullname, v_birthday, v_sex, v_about, v_photo)
    private final String UPDATE_PROFILE = " update profile  set"
            + " nickname = '%s',    fullname = '%s',   birthday =  %tD, "
            + "sex = %d, about = '%s', photo = null  where user_id = %d";
    // nickname,fullname,birthday,sex,about,photo
    private final String FIND_PROFILE = "select user_id, nickname, fullname, birthday, sex, about, photo from profile where user_id = %d";
    private final String FIND_PROFILES_BY_NAME = "select user_id, nickname, fullname, birthday, sex, about, photo from profile where fullname like '%s'";

    @Override
    public ProfileData findProfile(int userID) {
        // FIND_PROFILE
        ProfileData profile = new ProfileData();
        try {
            ResultSet RS = query.getRSForQuery(String.format(FIND_PROFILE, userID));
            while (RS.next()) {
                profile.setAbout(RS.getString("about"));
                // profile.setBirthday(RS.getDate("birthday"));
                profile.setFullname(RS.getString("fullname"));
                profile.setNickname(RS.getString("nickname"));
                profile.setPhoto(null);//temporary null then try to make
                profile.setSex(RS.getInt("sex"));
                profile.setUserId(RS.getInt("user_id"));
            }
            return profile;
        } catch (Exception e) {
            return profile;
            // return null
        }
    }

    @Override
    public boolean updateProfile(ProfileData profileData) {
        //UPDATE_PROFILE
        // nickname,fullname,birthday,sex,about,photo
        return query.executeUpdateStatement(String.format(UPDATE_PROFILE, profileData.getNickname(),
                profileData.getFullname(), profileData.getBirthday(), profileData.getSex(), profileData.getAbout()));
    }

    @Override
    public boolean createProfile(ProfileData profileData) {
        return query.executeInsertStatement(String.format(INSERT_PROFILE, profileData.getUserId(),
                profileData.getNickname(), profileData.getFullname(), profileData.getBirthday(), profileData.getSex(), profileData.getAbout()));
    }

    @Override
    public Collection<ProfileData> selectProfileDataCollection(String Criteria) {
        ResultSet RS = query.getRSForQuery(String.format(FIND_PROFILES_BY_NAME, Criteria));

        ArrayList<ProfileData> friendsList = new ArrayList<ProfileData>();
        try {
            while (RS.next()) {
                ProfileData friendProfile = new ProfileData();
                friendProfile.setAbout(RS.getString("about"));
                friendProfile.setBirthday(RS.getDate("birthday"));
                friendProfile.setFullname(RS.getString("fullname"));
                friendProfile.setNickname(RS.getString("nickname"));
                friendProfile.setPhoto(null);//temporary null then try to make
                friendProfile.setSex(RS.getInt("sex"));
                friendProfile.setUserId(RS.getInt("user_id"));
                friendsList.add(friendProfile);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OraUser.class.getName()).log(Level.SEVERE, null,
                    ex);
            return null;
        }
        return friendsList;
    }
}
